package com.hippo.api.file.util;

import javax.imageio.stream.FileImageInputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class ReadFileUtil {
    public static void readFileToResponse(HttpServletResponse response, String filePath, String fileName){
        File file=new File(filePath);
        try (
                FileImageInputStream fs = new FileImageInputStream (file);
        ) {
            int streamLength = (int)fs.length();
            byte[] fileByte = new byte[streamLength];
            fs.read(fileByte,0,streamLength );
            response.setHeader("Content-Type","application/octet-stream");
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            response.getOutputStream().write(fileByte);
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
